package com.usian.common.exception;

import com.usian.model.base.BaseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@Log4j2
public class ExceptionCatch implements BaseResult {

    @ResponseBody
    @ExceptionHandler(Exception.class)
    public ResponseResult exception(Exception e){

        e.printStackTrace();
        log.error("catch exception:{}", e.getMessage());

        if (e instanceof NullPointerException){
            return ResponseResult.errorResult(AppHttpCodeEnum.Null_POINTER);
        }else if (e instanceof ClassCastException){
            return ResponseResult.errorResult(AppHttpCodeEnum.CLASS_CAST);
        }

        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }



}
